package com.abcdefg.lotterysystem.controller;

import com.abcdefg.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.abcdefg.lotterysystem.common.exception.ControllerException;
import com.abcdefg.lotterysystem.common.result.CommonResult;
import com.abcdefg.lotterysystem.common.utils.JacksonUtils;
import com.abcdefg.lotterysystem.controller.param.ShortMessageLoginParam;
import com.abcdefg.lotterysystem.controller.param.UserPasswordLoginParam;
import com.abcdefg.lotterysystem.controller.param.UserRegisterParam;
import com.abcdefg.lotterysystem.controller.result.BaseUserInfoResult;
import com.abcdefg.lotterysystem.controller.result.UserLoginResult;
import com.abcdefg.lotterysystem.controller.result.UserRegisterResult;
import com.abcdefg.lotterysystem.service.UserService;
import com.abcdefg.lotterysystem.service.VerificationCodeService;
import com.abcdefg.lotterysystem.service.dto.UserDTO;
import com.abcdefg.lotterysystem.service.dto.UserLoginDTO;
import com.abcdefg.lotterysystem.service.dto.UserRegisterDTO;
import com.abcdefg.lotterysystem.service.enums.UserIdentityEnum;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private VerificationCodeService verificationCodeService;

    /**
     * 注册
     *
     * @param param
     * @return
     */
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(
            @Validated @RequestBody UserRegisterParam param) {
        //打印日志
        logger.info("userRegister UserRegisterParam : {}", param);
        //调用Service进行调用
        UserRegisterDTO userRegisterDTO = userService.register(param);
        return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }


    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        UserRegisterResult userRegisterResult = new UserRegisterResult();
        if (userRegisterDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        userRegisterResult.setUserId(userRegisterDTO.getUserId());
        return userRegisterResult;
    }
    /**
     * 发送验证码服务
     *
     * @param email
     * @return
     */
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String email) {
        logger.info("sendVerificationCode email:{}", email);
        verificationCodeService.sendVerificationCode(email);
        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 密码登录
     *
     * @param param
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userPasswordLogin(
            @Validated @RequestBody UserPasswordLoginParam param){
        logger.info("userPasswordLogin param:{}", JacksonUtils.writeValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    private UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDTO) {
        if(userLoginDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult userLoginResult = new UserLoginResult();
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setIdentity(userLoginDTO.getIdentity().name());
        return userLoginResult;
    }

    /**
     * 短信验证码登录
     *
     * @param param
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> shortMessageLogin(
            @Validated @RequestBody ShortMessageLoginParam param) {
        logger.info("shortMessageLogin ShortMessageLoginParam:{}",
                JacksonUtils.writeValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    @RequestMapping("/base-user/find-list")
    public CommonResult<List<BaseUserInfoResult>> findBaseUserInfo(String identity){
        logger.info("findBaseUserInfo identity:{}", identity);
         List<UserDTO> userDTOList=userService.findUserInfo(UserIdentityEnum.forName(identity));
         return CommonResult.success(convertToList(userDTOList));
    }

    private List<BaseUserInfoResult> convertToList(List<UserDTO> userDTOList) {
        if (CollectionUtils.isEmpty(userDTOList)) {
            return Arrays.asList();
        }

        return userDTOList.stream()
                .map(userDTO -> {
            BaseUserInfoResult result = new BaseUserInfoResult();
            result.setUserId(userDTO.getUserId());
            result.setUserName(userDTO.getUserName());
            result.setIdentity(userDTO.getIdentity().name());
            return result;
        }).collect(Collectors.toList());
    }

}
